﻿#pragma once

#include"HashTable.h"


namespace XmLife
{
	template <class k , class v, class Hash = HashBucket::HashFunc<k>>
	class  unordered_map
	{
		struct MapKeyOfT
		{
			const k& operator()(const pair<k, v>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef  typename HashBucket::HashTable<k,pair<k,v>,Hash, MapKeyOfT>::iterator iterator;

		iterator begin()
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		pair<iterator, bool> insert(const pair<k, v>& kv)
		{
			return _ht.insert(kv);
		}

		v& operator[](const k& kv)
		{
			pair<iterator, bool> ret = _ht.insert(make_pair(kv, v()));
			return ret.first->second;
		}
	private:
		HashBucket::HashTable<k, pair<k, v>, Hash, MapKeyOfT> _ht;
	};


	void test_map()
	{
		unordered_map<string, string> dict;
		dict.insert(make_pair("sort", "排序"));
		dict.insert(make_pair("string", "字符串"));
		dict.insert(make_pair("left", "左边"));

		unordered_map<string, string>::iterator it = dict.begin();
		while (it != dict.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}

		for (auto x : dict)
		{
			cout << x.first << "::" << x.second << endl;

		}
		cout << endl;

		unordered_map<string, int> countMap;
		//string arr[] = { "ƻ", "", "ƻ", "", "ƻ", "ƻ", "", "ƻ", "㽶", "ƻ", "㽶" };
		/*for (auto e : arr)
		{
			countMap[e]++;
		}

		for (auto& kv : countMap)
		{
			cout << kv.first << ":" << kv.second << endl;
		}*/
	}
}